var convID;
var maxID;
var IDs;
function chat(ID, ids) {
    var count = 0;
    maxID = ID;
    // console.log(maxID);
    var timer = $.timer(
            function () {
                var data = {maxID: maxID};
                $.ajax({
                    type: "POST",
                    url: "/ajax/getMessage",
                    dataType: "json",
                    data: data,
                    success: function (json) {
                        if (json) {
                            if (parseInt(json.AnswerID) > maxID && $.inArray(parseInt(json.ConversationID), ids) !== -1) {
                                maxID = parseInt(json.AnswerID);
                                var divClass = (json.SendByCustomer === "1") ? "customer" : "seller newMessage";
                                $("#AllConversation" + json.ConversationID).
                                        children(".messages").append('<div class="' + divClass + '" ><input type="hidden" value="' + json.AnswerID + '">' + json.AdditionDate + '<br/>' + json.Content + "</div>");
                                if (json.SendByCustomer === "0") {
                                    $.playSound('/img/message');
                                    var amount = parseInt($("#newMessage" + json.ConversationID).children("span").text()) + 1;
                                    var amuontAll = parseInt($("#newMessagesContact").text()) + 1;
                                    $("#newMessage" + json.ConversationID).show();
                                    $("#newMessage" + json.ConversationID).children("span").text(amount);
                                    $("#newMessagesContact").show();
                                    $("#newMessagesContact").text(amuontAll);
                                    if ($("#AllConversation" + json.ConversationID).children(".messages").children().length > 15) {
                                        $("#AllConversation" + json.ConversationID).children(".messages").children().eq(0).remove();
                                    }
                                    $("#AllConversation" + json.ConversationID).
                                            children(".messages").scrollTop(10000000);
                                }
                            }

                        }
                        //console.log(json);
                    },
                    error: function (data) {
                        //  console.log(data.responseText);
                    }
                });
            },
            5000,
            true
            );
}
function initMessages(ids) {
    IDs = ids;
    $.each(ids, function (index, value) {
        $("#conversation" + value).Editor("messageConv" + value, "");
        //console.log(value);
    });
    $("#newQuestion").Editor("newQuestionConv", "");
    $(".newconv").click(function () {
        $(this).next().removeClass("hidden");
    });
}
function send(data) {
 $.ajax({
        type: "POST",
        url: "/ajax/sendMessage",
        dataType: "json",
        data: data,
        success: function (json) {
            maxID++;
            $("#AllConversation" + json.ConversationID).
                    children(".messages").append('<div class="customer" ><input type="hidden" value="' + json.AnswerID + '">' + json.AdditionDate + '<br/>' + json.Content + "</div>");
            if ($("#AllConversation" + json.ConversationID).children(".messages").children().length > 15) {
                $("#AllConversation" + json.ConversationID).children(".messages").children().eq(0).remove();
            }

            $("#AllConversation" + json.ConversationID).
                    children(".messages").scrollTop(10000000);
        },
        error: function (data) {
            //  console.log(data.responseText);
        }
    });
}
function sendMessage() {
    $("textarea").unbind("keydown");
    $(".sendMessage").click(function(){
            var textarea = $(this).parent().find("textarea");
    var message = textarea.val();
    textarea.text("");
    textarea.val("");
    textarea.html("");
    textarea.next("div").text("");
    var convID = $(this).parent().children("input").val();
    var data = {
        message: message,
        convID: convID
    }; 
    send(data);
    });
    $("textarea").keydown(function (event) {
        if (event.which === 13) {
            if (event.shiftKey) {
                    $(this).selection('insert', {text: '<br/>', mode: 'before'});
            }
            else{
                   var message = $(this).val();
                    $(this).text("");
                    $(this).val("");
                    $(this).html("");
                  $(this).next("div").text("");
                    var convID = $(this).parent().parent().parent().children("input").val();                   
                    var data = {
                        message: message,
                        convID: convID
                    }; 
                    send(data);
            }            
        }
    });
}
function beginConversation() {
    $(".beginConv").click(function () {
        var textarea = $(this).parent().find("textarea");
        var message = textarea.val();
        var topic = $(this).parent().children("div").children("input").val();
        var data = {
            message: message,
            topic: topic
        };
        $.ajax({
            type: "POST",
            url: "/ajax/beginConv",
            dataType: "json",
            data: data,
            success: function (json) {
                window.location.href = window.location.href;
                // textarea.text("");
            },
            error: function (data) {
                //  console.log(data.responseText);
            }
        });
    });
}
function markAsRead() {
    $(".newMessage").click(function () {

    });
}
$(document).ready(function () {
    $("body").on("click", ".newMessage", function (event) {
        convID = $(this).parent().parent().children("input").val();
        var messageID = $(this).children("input").val();
        var data = {
            messageID: messageID,
            convID: convID
        };
        $.ajax({
            type: "POST",
            url: "/ajax/markAsRead",
            dataType: "json",
            data: data,
            success: function (json) {
            },
            error: function (data) {
            }
        });
        var amount = parseInt($("#newMessage" + convID).children("span").text()) - 1;
        var amuontAll = parseInt($("#newMessagesContact").text()) - 1;
        if (amount <= 0) {
            $("#newMessage" + convID).hide();
        }
        $("#newMessage" + convID).children("span").text(amount);
        if (amuontAll <= 0) {
            $("#newMessagesContact").hide();
        }
        $("#newMessagesContact").text(amuontAll);
        $(this).removeClass("newMessage");
    });
    $(".convTitle").on("click", function () {
        var all = $(this).next("div");
        var messages = all.children(".messages");
        messages.scrollTop(1000000);
    });
});